// +----------------------------------------------------------------------
// | Project:   Android-Commons
// +----------------------------------------------------------------------
// | CreateTime: 12/10/2015 13:57 下午
// +----------------------------------------------------------------------
// | Author:     xab(xab@xabad.cn)
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
package cn.xabad.commons.sample.ui.presenterimp;

import cn.xabad.commons.converter.GsonCallback;
import cn.xabad.commons.converter.RetrofitError;
import cn.xabad.commons.converter.XsonCallback;
import cn.xabad.commons.converter.XsonObject;
import cn.xabad.commons.sample.R;
import cn.xabad.commons.sample.interactors.MainInteractor;
import cn.xabad.commons.sample.model.StudentInfo;
import cn.xabad.commons.sample.tools.L;
import cn.xabad.commons.sample.ui.commons.BasePresenterImp;
import cn.xabad.commons.sample.ui.features.IMain;
import cn.xabad.commons.sample.ui.presenter.MainPresenter;
import cn.xabad.commons.tools.RandomU;
import rx.Observable;

/**
 * DESC   :
 * AUTHOR : Xabad
 */
public class MainPresenterImp extends BasePresenterImp implements MainPresenter {
    MainInteractor interactor;
    IMain viewInterface;


    public MainPresenterImp(IMain viewInterface, MainInteractor mainInteractor) {
        super();
        this.viewInterface = viewInterface;
        this.interactor = mainInteractor;
    }


    @Override
    public void regist() {
        String u = RandomU.randWord(1);
        viewInterface.showLoadingDialog(R.string.loading);
        interactor.regist(u + "@xabad.cn", "qqqqqq", "qqqqqq", null, new XsonCallback() {
            @Override
            public void onSuccess(XsonObject s) {
                L.i(s.contians("status", "success"));
                viewInterface.hideLoadingDialog();
            }


            @Override
            public void onFail(RetrofitError error) {
                viewInterface.hideLoadingDialog();
                viewInterface.interError(error);
            }
        });
    }


    @Override
    public void login() {
        String u = RandomU.randWord(1);
        viewInterface.showLoadingDialog(R.string.loading);
        interactor.login("a@xabad.cn", "qqqqqq", new XsonCallback() {
            @Override
            public void onSuccess(XsonObject s) {
                viewInterface.hideLoadingDialog();
                if (s.equals("code", "success")) {
                    preferenceU.saveString("access_token", s.getString("access_token"));
                    preferenceU.saveString("student_id", s.getString("id"));
                } else {
                    if (s.equals("code", "failure")) {
                        viewInterface.onTip(s.getString("msg"));
                    }
                }
            }


            @Override
            public void onFail(RetrofitError error) {
                viewInterface.hideLoadingDialog();
                viewInterface.interError(error);
            }
        });
    }


    @Override
    public void userme() {
        viewInterface.showLoadingDialog(R.string.loading);
        interactor.userme(preferenceU.getString("access_token"), new GsonCallback<StudentInfo>() {
            @Override
            public void onSuccess(StudentInfo studentInfo) {
                viewInterface.hideLoadingDialog();
                if (studentInfo == null) {
                    L.line();
                    return;
                }
                if (studentInfo.getFirst_login()) {
                    L.i("领5000，金币去~~~~");
                }else {

                }
            }


            @Override
            public void onFail(RetrofitError error) {
                viewInterface.hideLoadingDialog();
                viewInterface.interError(error);
            }
        });
    }

    public void syncBaseInfo(StudentInfo studentInfo){
        Observable.just(studentInfo.getDataRefresh());
        Observable.just(studentInfo.getQueueConfig());
    }
}
